import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 读取CSV文件数据
data = pd.read_csv('600887历史数据.csv', encoding='gbk')

# 提取收盘价数据
historical_prices = data['收盘价'].values

# 计算日收益率
daily_returns = np.diff(historical_prices) / historical_prices[:-1]

# 计算平均收益率和标准差
mean_return = np.mean(daily_returns)
std_dev = np.std(daily_returns)

# 模拟未来250个交易日的股价
future_days = 250
simulated_prices = np.zeros(future_days)
for i in range(future_days):
    return_for_day = np.random.normal(mean_return, std_dev)
    simulated_prices[i] = historical_prices[-1] * (1 + return_for_day)

# 计算20次模拟的平均股价路径
average_simulated_price = np.mean([simulated_prices for _ in range(20)], axis=0)

# 绘制实际股价和预测股价
plt.figure(figsize=(12, 6))
plt.plot(historical_prices, label='Actual Price', color='blue')

# 绘制平均预测股价路径
plt.plot(np.arange(future_days), average_simulated_price, label='Average Simulated Price', color='red')

# 绘制所有模拟的股价路径
for _ in range(20):
    plt.plot(np.arange(future_days), simulated_prices, color='gray', alpha=0.1)

# 绘制置信区间
plt.fill_between(np.arange(future_days), 
           average_simulated_price - 1.96 * std_dev, 
           average_simulated_price + 1.96 * std_dev, color='gray', alpha=0.5)

plt.title('伊利股票价格预测')
plt.xlabel('Days')
plt.ylabel('Price')
plt.legend()
plt.show()